var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    examSetting: {

    },
    isExamFinished: false,
    totalScore: 0,//考试得分

    btnTitle: '下一题',
    isStarted: false,
    current: 0,
    total: 160,
    remainTime: 0,
    minutes: 90,
    subjectList: [

    ]
   

  },

  startexam: function () {
    this.setData({ 'isStarted': true });
    var me = this;
    app.util.request({
      url: "entry/wxapp/getExamSubjects",
      data: {
        m: "rs_anwser",
        subjectType: 'com'
      },
      success: function (data) {
        var tempSubjectList = data.data.data;

        var subjectList = [];
        for (var s in tempSubjectList) {
          for (var i = 0; i < tempSubjectList[s].length; i++) {
            tempSubjectList[s][i]['type'] = s;
            tempSubjectList[s][i]['sIndex'] = i;
            subjectList.push(tempSubjectList[s][i]);
          }
        }
        me.setData({
          subjectList: subjectList,
          total: subjectList.length,
          singleCount:tempSubjectList.single.length,
          mutilpleCount:tempSubjectList.mutilple.length,
          judgeCount:tempSubjectList.judge.length,
          questionCount:tempSubjectList.question.length
        })
      }
    })
  },

  updateSubjectList: function (data) {

    this.setData({ subjectList: data });
  },


  /**
   * 选择题目选项
   */
  selectOption: function (e) {


    var subject = getApp().subjectPage.getCurrentSubject(this);
    var index = e.currentTarget.dataset.index;

    var options = subject.options;

    if(subject.type=='single'){
      for (var i = 0; i < options.length; i++) {
        options[i].resultClass = '';
      }
      options[index]['resultClass'] = 'option-select';
      subject.options = options;
     
      subject.selectOption = options[index].label;
    
    } else if (subject.type == 'mutilple') {

      var resultClass = typeof (options[index]['resultClass']) == 'undefined' ? '' : options[index]['resultClass'];
      var selectOption = typeof (subject.selectOption) == 'undefined' ? [] : subject.selectOption;
      var label = options[index]['label'];
      if (resultClass == '') {
        resultClass = 'option-select';
        selectOption.push(label);
      } else {
        resultClass = '';
        selectOption.splice(selectOption.indexOf(label) - 1, 1);
      }
      
      options[index]['resultClass'] = resultClass;
      subject.options = options;
      subject.selectOption = selectOption; 
    }else if(subject.type == 'judge'){

      var value = e.currentTarget.dataset.value;
      if (value == 1) {
        subject.wrongClass = "";
        subject.rightClass = "option-select";
      } else {
        subject.rightClass = "";
        subject.wrongClass = "option-select";
      }
      subject.result = value == 1 ? '正确' : '错误';


    }
  
    getApp().subjectPage.updateCurrentSubject(this, subject);
   




   

  },

  updateCurrent: function (data) {
    var current = data.detail;
    this.setData({ current: current });
   
  },

  updateRemainTime: function (e) {
    this.setData({ remainTime: e.detail.remainTime });

  },
  updateSubjectList: function (e) {

  },

  selelectAnswerImg:function(e){
    var me = this;
    var current = e.target.dataset.current;
    var subjectList = this.data.subjectList;
    var answerImgList = typeof (subjectList[current].answerImgList) == 'undefined' ? []:subjectList[current].answerImgList;
    var localAnswerImgList = typeof (subjectList[current].localAnswerImgList) == 'undefined' ? [] : subjectList[current].localAnswerImgList;
    wx.chooseImage({
      count:1,
      success: function (res) {
        localAnswerImgList.push(res.tempFilePaths[0]);
        subjectList[current].localAnswerImgList = localAnswerImgList;
        wx.uploadFile({
          url: app.util.url('entry/wxapp/uploadAnswerImg'),
          filePath: res.tempFilePaths[0],
          name: 'answerImg',
          formData: {
            m: 'rs_anwser',
            id: subjectList[current].id
          },
          success: function (data) {
            console.log(data);
            var dataObj = JSON.parse(data.data);
            answerImgList.push(dataObj.data);
            subjectList[current].answerImgList = answerImgList;
            console.log(subjectList);
            me.setData({ subjectList: subjectList });
          }
        })
       
      }
    })

  },

  updateTotalScore: function (e) {
    this.setData({ totalScore: e.detail, isExamFinished: true });
  },
  changeSubject: function (e) {
    if (e.detail.current == this.data.subjectList.length - 1) {
      this.setData({ btnTitle: '交卷' });
    } else if (e.detail.current == this.data.subjectList.length - 2) {
      this.setData({ btnTitle: '下一题' });
    }
    this.setData({ current: e.detail.current });
  },

  /**
   * 查看成绩
   */
  showScore:function(e){
     wx.navigateTo({
       url: '/rs_anwser/pages/score/score?subjectType=com',
     })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var me = this;
    app.util.request({
      url: 'entry/wxapp/getExamSetting',
      data: {
        m: 'rs_anwser',
        subjectType: 'com'
      },
      success: function (data) {
        var temp = data.data.data;
        var examSetting = {
           duration: temp.duration, 
           title: temp.title, 
           singlePerScore: temp.setting.com.single_per_score,
           singleCount: temp.setting.com.single_count,
           mutilplePerScore: temp.setting.com. mutilple_per_score,
           mutilpleCount: temp.setting.com. mutilple_count,
           judgeerScore: temp.setting.com.judge_per_score,
           judgeCount: temp.setting.com.judge_count,
           questionScore: temp.setting.com.question_per_score,
           questionCount: temp.setting.com.question_count,
        };
        me.setData({ examSetting: examSetting });

        me.setData({ 'minites': temp.duration, 'remainTime': getApp().subjectPage.getRemainTime(temp.duration * 60) });


      }
    })



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})




